
package com.iocup.keybastion.authorize.parser;



import com.iocup.keybastion.annotion.HasPermission;
import com.iocup.keybastion.authorize.decision.DecisionType;
import com.iocup.keybastion.authorize.element.AuthElement;
import com.iocup.keybastion.annotion.HasAuthorization;
import com.iocup.keybastion.annotion.HasRole;

import java.util.HashMap;
import java.util.Map;

/**
 * @author xyjxust
 * @create 2022/3/2 13:44
 **/
public class HasAuthorizationElementParser implements AnnotationElementParser<HasAuthorization> {

    @Override
    public AuthElement parser(String key, HasAuthorization basicAuth) {
        Map<String,String> permission = new HashMap<>();
        permission.put(HasPermission.VALUE,String.join(",",basicAuth.permission().value()));
        permission.put(HasPermission.LOGICAL,basicAuth.permission().logical().name());
        Map<String,String> roles = new HashMap<>();
        roles.put(HasRole.VALUE,String.join(",",basicAuth.role().value()));
        roles.put(HasRole.LOGICAL,basicAuth.role().logical().name());
        return AuthElement.build()
                .code(key)
                .type(getType())
                .putParam(HasAuthorization.ROLE, roles)
                .putParam(HasAuthorization.PERMISSION, permission)
                .putParam(HasAuthorization.LOGICAL, basicAuth.logical().name());
    }

    @Override
    public String getType() {
         return DecisionType.hasAuthorization.name();
    }
}
